﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Xml.Linq;
namespace WPFExcelReport
{
    public partial class JLTAB2INTEX : BaseFunction
    {
        public DataSet SetData = new DataSet();
        public DataSet SetDispo = new DataSet();
        public DateTime tomorrow = DateTime.Now.AddDays(1);
        public DateTime yesterday = DateTime.Now.AddDays(-1);
        public string ITXMANDANT = "00", ITXDBVERSION = "50.24", ITXUSER = "ITXVVVK ", ITXDATE = "20120723", ITXVKKRS = "C0", ITXVKLFD = "100227";

        public string today = "";
        public string Right(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str.Substring(leng - num, num);
            }
            catch (Exception ex)
            {
            }
            return str;
        }
        public string Left(string str, int num)
        {
            try
            {
                int leng = str.Length;
                str = str + "                                                                                ";
                str = str.Substring(0, num);
            }
            catch (Exception ex)
            {

            }
            return str;
        }
        OracleCommand or1;
        public OracleConnection OracleDB;
        INTEXDBC INTEXDBC;

        public JLTAB2INTEX(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {

            OracleDB = _OracleDB;
            proxy = _proxy;
        }
        /// <summary>
        /// 通过客户0实现不同的组合剪板条件设定。
        /// </summary>
        /// <param name="xdoc"></param>
        public void Execute(XDocument xdoc = null)
        {

            string KRS="", LFD="", POS="";
       
            if (INTEXDBC == null)
            {
                INTEXDBC = new INTEXDBC(OracleDB, proxy);
            }
            #region==ABPOS====

            if (SetData.Tables.Contains("ABPOS"))
            {
                foreach (DataRow row in SetData.Tables["ABPOS"].Rows)
                {
                    KRS = row["ABPAUFKRS"].ToString();
                    LFD = row["ABPAUFLFD"].ToString();
                    POS = row["ABPAUFPOS"].ToString();
                    EventDispatcher.Instance.RaiseStatus("开始保存ABPOS:" + KRS + "_" +LFD+ "_" +POS , StatusEventType.RunningProgress);
                    INTEXDBC.SaveABPOS(row);
                }
            }
            #endregion
            #region==ABKOPF====

            if (SetData.Tables.Contains("ABKOPF"))
            {
                foreach (DataRow row in SetData.Tables["ABKOPF"].Rows)
                {             
                    EventDispatcher.Instance.RaiseStatus("开始保存ABPOS:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                    INTEXDBC.SaveABKOPF(row);
                }
            }
            #endregion
            #region==ABPR====

            if (SetData.Tables.Contains("ABPR"))
            {
                foreach (DataRow row in SetData.Tables["ABPR"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("开始保存ABPOS:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                    INTEXDBC.SaveABPR(row);
                }
            }
            #endregion
            #region==VKPOS====

            if (SetData.Tables.Contains("VKPOS"))
            {
                foreach (DataRow row in SetData.Tables["VKPOS"].Rows)
                {
         
                    INTEXDBC.UpdateVKPOS(row);
                }
            }
            #endregion

            #region===ABAUFM===ok

            if (SetData.Tables.Contains("ABAUFM"))
            {
                foreach (DataRow row in SetData.Tables["ABAUFM"].Rows)
                {
                    EventDispatcher.Instance.RaiseStatus("开始保存ABAUFM:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                    INTEXDBC.SaveABAUFM(row);
                }
            }
            #endregion

            #region===ABFOLGE===ok
            if (SetData.Tables.Contains("ABFOLGE"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存ABFOLGE:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["ABFOLGE"].Rows)
                {
                    INTEXDBC.SaveABFOLGE(row);
                }
            }

            #endregion
            #region===ABARTE===ok
            if (SetData.Tables.Contains("ABARTE"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存ABARTE:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["ABARTE"].Rows)
                {
                    INTEXDBC.SaveABARTE(row);
                }
            }
            #endregion
            #region==WDECK====Hope

            if (SetData.Tables.Contains("WDECK"))
            {
                EventDispatcher.Instance.RaiseStatus("开始保存WDECK:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["WDECK"].Rows)
                {
                    INTEXDBC.SaveWDECK(row);
                }
            }
            #endregion

            #region==BAAUF====    ok
            if (SetData.Tables.Contains("BAAUF"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存BAAUF:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["BAAUF"].Rows)
                {
                    INTEXDBC.SaveBAAUF(row,null);
                }
            }
            #endregion
            
            #region==GAAUF====    ok
            if (SetData.Tables.Contains("GAAUF"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存GAAUF:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["GAAUF"].Rows)
                {
                    INTEXDBC.SaveGAAUF(row);
                }
            }
            #endregion

            #region===BABST===ok

            if (SetData.Tables.Contains("BABST"))//ok
            {
                EventDispatcher.Instance.RaiseStatus("开始保存BABST:" + KRS + "_" + LFD + "_" + POS, StatusEventType.RunningProgress);
                foreach (DataRow row in SetData.Tables["BABST"].Rows)
                {
                    INTEXDBC.SaveBABST(row,null);
                }
            }
            #endregion

            EventDispatcher.Instance.RaiseStatus("保存完成", StatusEventType.StopProgress);

        }
        public void ReSet(DataSet setData, DataSet setDispo)
        {
            try
            {
                this.SetData = setData;
                this.SetDispo = setDispo;
                UserID = "INTEX   ";
                if (!this.SetData.Tables.Contains("INTEXMAP"))
                    return;
                if (this.SetData.Tables["INTEXMAP"].Rows.Count == 0)
                    return;
                DataRow dwIntex = this.SetData.Tables["INTEXMAP"].Rows[0];
                ITXMANDANT = dwIntex["ITXMANDANT"].ToString();
                ITXVKKRS = dwIntex["ITXVKKRS"].ToString();
                ITXVKLFD = dwIntex["ITXVKLFD"].ToString();
                ITXUSER = dwIntex["ITXUSER"].ToString();
                ITXDATE = dwIntex["ITXDATE"].ToString();
                ITXDBVERSION = dwIntex["ITXDBVERSION"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}